package org.soarsky.wealthchengdu.google.map;

import java.util.List;

import org.soarsky.wealthchengdu.entity.Location;
import org.soarsky.wealthchengdu.entity.MapSearchData;
import org.soarsky.wealthchengdu.entity.SearchItemData;

import android.os.Handler;
import android.os.Message;

public class MarkOPertorAssistant {
	private AbstractGoogleMap mAbstractGoogleMap = null;
	private List<SearchItemData> results;
	private boolean firstRun;
	private int index = 0;
	private MarkOperator markOperator;

	public MarkOPertorAssistant(MarkOperator markOperator) {
		this.markOperator = markOperator;
	}

	public void startDraw(MapSearchData datas) {
		results = datas.getResults();
		//handler.removeCallbacks(thread);
		index = 0;

		//handler.postDelayed(thread, 10);
		int size = results.size();
		for (int i = index; i < size; i++) {
			SearchItemData data = results.get(i);
			Location loc = data.getGeometry().getLocation();
			MarkPoint markPoint2 = new MarkPoint(i, Double.parseDouble(loc
					.getLat()), Double.parseDouble(loc.getLng()), null);
			
			markPoint2.setTitle(data.getName());
			// markPoint2.setContent(null);
			if(markOperator!=null){
				markOperator.addMarkOverlay(markPoint2);
			}
		
			// mAbstractGoogleMap.addMark(i, latitude, longitude, icon)
		}
		
	}

	Thread thread = new Thread() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int size = results.size();
			int msg = 0;
			for (int i = index; i < size; i++) {
				SearchItemData data = results.get(i);
				Location loc = data.getGeometry().getLocation();
				MarkPoint markPoint2 = new MarkPoint(i, Double.parseDouble(loc
						.getLat()), Double.parseDouble(loc.getLng()), null);
				markPoint2.setTitle(data.getName());
				// markPoint2.setContent(null);
				markOperator.addMarkOverlay(markPoint2);
				if ((i-index) > 20) {
					msg = 1;
					break;
				}
				index=i;
				handler.sendEmptyMessage(msg);
				// mAbstractGoogleMap.addMark(i, latitude, longitude, icon)
			}
		}

	};
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				handler.removeCallbacks(thread);
				break;
			case 1:
				//markOperator.
				handler.postDelayed(thread, 50);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};
}
